package managedbean.SaleLead; 
 
import entity.CRM.SaleLead; 
import entity.Employee;
import java.io.IOException;
import java.io.Serializable;
import java.util.List; 
import javax.ejb.EJB; 
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.Controller;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.CRM.SaleOrderBeanLocal;

@ManagedBean(name="saleLeadDataTableBean")
@ViewScoped 
public class ViewSaleLeadManagerBean implements Serializable { 
    @EJB 
    private SaleLeadBeanLocal saleLeadBeanLocal; 
    @EJB
    private CustomerBeanLocal customerSessionBeanLocal;
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    
    private SaleLead selectedSaleLead;
    
    private String statusMessage;
    private List<SaleLead> saleLeads;
    public ViewSaleLeadManagerBean() {} 
    
    public List<SaleLead> getSaleLeads() 
    { 
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee executive = controller.getEmployee();
        return saleLeadBeanLocal.getSalesLead(executive.getEmployeeId());
    }
    
    public SaleLead getSelectedSaleLead() {
        return selectedSaleLead;
    }
    public void setSelectedSaleLead(SaleLead selectedSaleLead) {
        this.selectedSaleLead = selectedSaleLead;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void editSaleLead(ActionEvent event) throws IOException{
        SaleLead selectedSaleLead = this.getSelectedSaleLead();
        if (selectedSaleLead != null){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSaleLead", selectedSaleLead);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editSaleLead.xhtml");                
        }
    }
    
    public void deleteSaleLead(ActionEvent event) throws IOException    {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();
        try
        {
            saleLeadBeanLocal.deleteSaleLead(selectedSaleLead.getRegistrationNumber());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleLead.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void addSaleInquiry(ActionEvent event) throws IOException {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();        
        if (selectedSaleLead != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleLead", selectedSaleLead);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleinquiry/addInquiryFromSaleLead.xhtml");                
        }
    }
    
     public void viewSaleInquiries(ActionEvent event) throws IOException
    {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSaleLead", selectedSaleLead);
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleLeadsInquiries.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void addSaleQuotation(ActionEvent event) throws IOException {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();        
        if (selectedSaleLead != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleLead", selectedSaleLead);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../salequotation/addQuotationFromSaleLead.xhtml");                
        }
    } 
    
    public void viewSaleQuotations(ActionEvent event) throws IOException
    {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSaleLead", selectedSaleLead);
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleLeadsQuotations.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void convertSaleLead(ActionEvent event) throws IOException
    {
        SaleLead selectedSaleLead = this.getSelectedSaleLead();
        try
        {
            ELContext elContext = FacesContext.getCurrentInstance().getELContext();
            Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
            Employee executive = controller.getEmployee();
            customerSessionBeanLocal.convertCustomerFromSaleLead(selectedSaleLead.getRegistrationNumber());
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSaleLead", selectedSaleLead);
            //FacesContext.getCurrentInstance().getExternalContext().redirect("viewAllSaleLeads.xhtml");
            
            statusMessage = "New Customer Converted Successfully"; 
 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Customer Result: " + statusMessage + ")", "")); 
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
} 
